package com.mini.rpc.consumer.controller;

import com.mini.rpc.common.RpcProperties;
import com.mini.rpc.consumer.annotation.RpcReference;
import com.mini.rpc.provider.facade.HelloFacade;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
public class HelloController {

    @Value("${rpc.registryAddr}")
    private String registryAddr;

    @Value("${rpc.registryLoadBalancer}")
    private String loadBalancer;

    @Resource
    private RpcProperties rpcProperties;

    @SuppressWarnings({"SpringJavaAutowiredFieldsWarningInspection", "SpringJavaInjectionPointsAutowiringInspection"})
    // @RpcReference(serviceVersion = "1.0.0", timeout = 6000, registryAddress = "101.34.88.247:2181")
    @RpcReference(serviceVersion = "1.0.0", registryType = "NACOS", registryAddress = "cvm-node02:8848")
    private HelloFacade helloFacade;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String sayHello() {
        return helloFacade.hello("mini rpc");
    }

    @RequestMapping(value = "responseError", method = RequestMethod.GET)
    public String testResponseError() {
        helloFacade.responseError();
        return "rpc call success";
    }

    @RequestMapping(value = "/responseCheckError", method = RequestMethod.GET)
    public String testResponseCheckError() throws Exception {
        helloFacade.responseCheckError();
        return "rpc call success";
    }

    @RequestMapping(value = "/responseTimeout", method = RequestMethod.GET)
    public String testResponseTimeout() {
        return helloFacade.responseTimeout();
    }

    @GetMapping(value = "/properties")
    public RpcProperties test02() {
        return rpcProperties;
    }
}
